import org.jsecurity.SecurityUtils
import rowingmanager.domain.User
import rowingmanager.domain.JsecUserRoleRel

class UserService {
    boolean transactional = false;
    def getAuthenticatedUser(){ 
      return User.findByEmail(SecurityUtils.subject.principal)
    }

    boolean hasRole(String name){
      def c = JsecUserRoleRel.createCriteria()
      def result = c {
        eq("user",this.getAuthenticatedUser())
        role{
          eq("name",name)
        }
      }
      if(result?.size() > 0)
        return true

      return false
    }

    String role(){  //just get us the name of the first role that it finds
      def result = JsecUserRoleRel.withCriteria {
          eq("user",this.getAuthenticatedUser())
       }
      return result?.role?.name
    }

}
